package com.yilin.tms.user.service.impl;

import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.JsonUtil;
import com.yilin.commons.util.MapUtil;
import com.yilin.tms.core.application.AppUtil;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.account.Contact;
import com.yilin.tms.core.entity.user.auth.*;
import com.yilin.tms.core.entity.user.organize.Cooperate;
import com.yilin.tms.core.entity.user.organize.MemberType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.entity.user.organize.UserRole;
import com.yilin.tms.user.mapper.auth.*;
import com.yilin.tms.user.service.*;
import com.yilin.tms.user.mapper.ContactMapper;
import com.yilin.tms.user.mapper.CooperateMapper;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MigrationService implements IMigrationService {
    protected Log log = LogFactory.getLog(this.getClass());
    @Resource
    private IOrganizeService organizeService;
    @Resource
    private IAccountService accountService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IMemberTypeService memberTypeService;

    @Resource
    private ContactMapper contactMapper;

    @Resource
    private IdCardMapper idCardMapper;
    @Resource
    private BusinessLicenseMapper businessLicenseMapper;
    @Resource
    private QualificationCertMapper qualificationCertMapper;
    @Resource
    private TransBusinessLicenseMapper transBusinessLicenseMapper;
    @Resource
    private DrivingLicenseMapper drivingLicenseMapper;

    @Resource
    private StampPersonalSignetMapper stampPersonalSignetMapper;
    @Resource
    private StampCompanySealMapper stampCompanySealMapper;
    @Resource
    private CooperateMapper cooperateMapper;

    //TODO:批量写入组织
    @Override
    public void insertOrganizeList(List<Map<String, Object>> mapList){
        List<Organize> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<Organize>>(){});
        for (Organize organize : list) {
            if (organize.getVirtualCoin()==null) organize.setVirtualCoin(0);
            if (organize.getMonthlyRating()==null) organize.setMonthlyRating(80d);
            if (organize.getMonthlyRatingTime()==null) organize.setMonthlyRatingTime(new Date());
        }
        organizeService.saveOrUpdateBatch(list,99999);
    }

    //TODO:批量写入用户
    @Override
    public void insertAccountList( List<Map<String, Object>> mapList){
        List<Account> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<Account>>(){});
        for (Account account : list) {
            String post = AppUtil.migrationPost("/user/migration/getOrderCount", MapUtil.generate(map -> map.put("accountId", account.getId())));
            int parseInt = Integer.parseInt(post==null?"0":post);
            account.setTotalClinchCount(parseInt);
        }
        accountService.saveOrUpdateBatch(list,99999);
    }

    //TODO:批量写入角色
    @Override
    public void insertRoleList(List<Map<String, Object>> mapList){
        List<UserRole> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<UserRole>>(){});
        for (UserRole role : list) { }
        roleService.saveOrUpdateBatch(list,99999);
    }

    //TODO:批量写入会员类型
    @Override
    public void insertMemberTypeList(List<Map<String, Object>> mapList){
        List<MemberType> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<MemberType>>(){});
        for (MemberType memberType : list) {
            if (memberType.getElectronicFenceRange()==null) memberType.setElectronicFenceRange(1000);
        }
        memberTypeService.saveOrUpdateBatch(list,99999);
    }

    //TODO:批量写入联系人
    @Override
    public void insertContactList(List<Map<String, Object>> mapList){
        List<Contact> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<Contact>>(){});
        for (Contact contact : list) {
            contactMapper.insert(contact);
        }
    }

    //TODO:批量写入合作关系
    @Override
    public void insertCooperateList(List<Map<String, Object>> mapList){
        List<Cooperate> list = JsonUtil.jsonToObject(JsonUtil.objectToJson(mapList),new TypeToken< List<Cooperate>>(){});
        for ( Cooperate contact: list)  cooperateMapper.insert(contact);
    }

    //TODO:批量写入认证
    @Override
    public <T> void insertAuthList(List<Map<String, Object>> mapList, Class<T> cls){
        for (Map<String, Object> auth : mapList) {
            if (cls.equals(IdCard.class)) idCardMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<IdCard>(){}));
            if (cls.equals(BusinessLicense.class)) businessLicenseMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<BusinessLicense>(){}));
            if (cls.equals(QualificationCert.class)) qualificationCertMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<QualificationCert>(){}));
            if (cls.equals(TransBusinessLicense.class)) transBusinessLicenseMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<TransBusinessLicense>(){}));
            if (cls.equals(DrivingLicense.class)) drivingLicenseMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<DrivingLicense>(){}));
            if (cls.equals(StampPersonalSignet.class)) stampPersonalSignetMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<StampPersonalSignet>(){}));
            if (cls.equals(StampCompanySeal.class)) stampCompanySealMapper.insert(JsonUtil.mapToObject(auth,new TypeToken<StampCompanySeal>(){}));
        }
    }

}
